package com.gator.rabbitmq.pablo_rabbitmq.boot.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author PABLO
 * @Date 2022/6/6 11:47
 * @Desc
 */
@Configuration
public class TTLConfig {
    //普通交换机
    public static final String NORMAL_EXCHANGE_NAME = "normal_exchange";
    //死信交换机
    public static final String DEAD_EXCHANGE_NAME = "dead_exchange";
    //普通队列AB
    public static final String NORMAL_QUEUE_NAME_A = "normal_queue_a";
    public static final String NORMAL_QUEUE_NAME_B = "normal_queue_b";
    //不指定ttl的队列,通用队列
    public static final String NORMAL_QUEUE_NAME_C = "normal_queue_c";
    //死信队列
    public static final String DEAD_QUEUE_NAME = "dead_queue";


    @Bean
    public DirectExchange normalExchange() {
        return new DirectExchange(NORMAL_EXCHANGE_NAME);
    }

    @Bean
    public DirectExchange deadExchange() {
        return new DirectExchange(DEAD_EXCHANGE_NAME);
    }

    @Bean
    public Queue normalQueueA() {
        Map<String, Object> arguments = new HashMap<>();
        //设置死信队列交换机
        arguments.put("x-dead-letter-exchange", DEAD_EXCHANGE_NAME);
        //设置死信队列路由key
        arguments.put("x-dead-letter-routing-key", "dead_key");
        //该队列TTL10S
        arguments.put("x-message-ttl", 10000);
        return QueueBuilder.durable(NORMAL_QUEUE_NAME_A).
                withArguments(arguments).
                build();
    }

    @Bean
    public Queue normalQueueB() {
        Map<String, Object> arguments = new HashMap<>();
        //设置死信队列交换机
        arguments.put("x-dead-letter-exchange", DEAD_EXCHANGE_NAME);
        //设置死信队列路由key
        arguments.put("x-dead-letter-routing-key", "dead_key");
        //该队列TTL30S
        arguments.put("x-message-ttl", 30000);
        return QueueBuilder.durable(NORMAL_QUEUE_NAME_B).
                withArguments(arguments).
                build();
    }

    //该队列不指定ttl时间，由生产者指定
    @Bean
    public Queue normalQueueC() {
        Map<String, Object> arguments = new HashMap<>();
        //设置死信队列交换机
        arguments.put("x-dead-letter-exchange", DEAD_EXCHANGE_NAME);
        //设置死信队列路由key
        arguments.put("x-dead-letter-routing-key", "dead_key");

        return QueueBuilder.durable(NORMAL_QUEUE_NAME_C).
                withArguments(arguments).
                build();
    }

    //死信队列
    @Bean
    public Queue deadQueue() {
        return QueueBuilder.durable(DEAD_QUEUE_NAME).
                build();
    }

    //普通交换机和队列A绑定
    @Bean
    public Binding queueABindNormalExchange(@Qualifier("normalQueueA") Queue normalQueueA
            , @Qualifier("normalExchange") DirectExchange normalExchange) {
        return BindingBuilder.bind(normalQueueA).to(normalExchange).with("qa");
    }

    //普通交换机和队列B绑定
    @Bean
    public Binding queueBBindNormalExchange(@Qualifier("normalQueueB") Queue normalQueueB
            , @Qualifier("normalExchange") DirectExchange normalExchange) {
        return BindingBuilder.bind(normalQueueB).to(normalExchange).with("qb");
    }

    //普通交换机和队列C绑定
    @Bean
    public Binding queueCBindNormalExchange(@Qualifier("normalQueueC") Queue normalQueueC
            , @Qualifier("normalExchange") DirectExchange normalExchange) {
        return BindingBuilder.bind(normalQueueC).to(normalExchange).with("qc");
    }


    //死信交换机和死信队列绑定
    @Bean
    public Binding deadQueueBindNormalExchange(@Qualifier("deadQueue") Queue deadQueue
            , @Qualifier("deadExchange") DirectExchange deadExchange) {
        return BindingBuilder.bind(deadQueue).to(deadExchange).with("dead_key");
    }

}
